package com.travel.mapper.content;

import com.travel.model.entity.service.Destination;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;
//目的地mapper 查询目的地 插入目的地 更新目的地 删除目的地 

@Mapper
public interface DestinationMapper {
    
    @Select("SELECT * FROM destinations WHERE id = #{id}")
    @Results({
        @Result(property = "user", column = "user_id", 
            one = @One(select = "com.travel.mapper.user.UserMapper.findById"))
    })
    Optional<Destination> findById(@Param("id") Long id);
    
    @Insert("INSERT INTO destinations(name, country, city, description, image_url, status, " +
            "latitude, longitude, best_season, climate, created_time, updated_time) " +
            "VALUES(#{name}, #{country}, #{city}, #{description}, #{imageUrl}, #{status}, " +
            "#{latitude}, #{longitude}, #{bestSeason}, #{climate}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(Destination destination);
    
    @Update("UPDATE destinations SET name=#{name}, country=#{country}, city=#{city}, " +
            "description=#{description}, image_url=#{imageUrl}, status=#{status}, " +
            "latitude=#{latitude}, longitude=#{longitude}, best_season=#{bestSeason}, " +
            "climate=#{climate}, updated_time=NOW() WHERE id=#{id}")
    void update(Destination destination);
    
    @Delete("DELETE FROM destinations WHERE id = #{id}")
    int delete(Destination destination);
    
    // 基本查询方法
    @Select("SELECT * FROM destinations WHERE status = 1")
    List<Destination> findAllActive();
    
    @Select("SELECT * FROM destinations WHERE city = #{city}")
    List<Destination> findByCity(@Param("city") String city);
    
    @Select("SELECT * FROM destinations WHERE status = #{status}")
    List<Destination> findByStatus(@Param("status") Integer status);
    
    @Select("SELECT * FROM destinations WHERE city = #{city} AND status = #{status}")
    List<Destination> findByCityAndStatus(@Param("city") String city, @Param("status") Integer status);
    
    // 搜索相关方法
    @Select("SELECT * FROM destinations WHERE " +
            "name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "description LIKE CONCAT('%', #{keyword}, '%') OR " +
            "city LIKE CONCAT('%', #{keyword}, '%')")
    List<Destination> searchDestinations(@Param("keyword") String keyword);
    
    @Select("SELECT * FROM destinations WHERE " +
            "name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "description LIKE CONCAT('%', #{keyword}, '%') OR " +
            "city LIKE CONCAT('%', #{keyword}, '%') " +
            "AND status = #{status}")
    List<Destination> searchDestinationsByStatus(
        @Param("keyword") String keyword, 
        @Param("status") Integer status
    );
} 